package com.hkbigdata.window;

import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.api.windowing.windows.Window;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @author liuanbo
 * @creat 2024-04-25-14:36
 * @see 2194550857@qq.com
 */
public class Flink06_Sliding_ReduceFunction_Windows {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);

        env.socketTextStream("hadoop102", 9999)
                .flatMap((String data, Collector<Tuple2<String, Integer>> out) -> {
                    String[] split = data.split(",");
                    Stream<String> stream = Arrays.stream(split);
                    stream.forEach(word -> out.collect(Tuple2.of(word, 1)));
                }).returns(Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(data -> data.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(10)))
/*                .reduce(new ReduceFunction<Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> reduce(Tuple2<String, Integer> value1, Tuple2<String, Integer> value2) throws Exception {
                        return Tuple2.of(value1.f0, value1.f1 + value2.f1);
                    }
                })*/
                /*.aggregate(new AggregateFunction<Tuple2<String, Integer>, Long, Long>() {
                    @Override
                    public Long createAccumulator() {
                        return 0L;
                    }

                    @Override
                    public Long add(Tuple2<String, Integer> value, Long accumulator) {
                        return accumulator + value.f1;
                    }

                    @Override
                    public Long getResult(Long accumulator) {
                        return accumulator;
                    }

                    @Override
                    public Long merge(Long a, Long b) {
                        return a + b;
                    }
                })*/
                /*           .aggregate(new AggregateFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, Tuple2<String, Integer>>() {
                               @Override
                               public Tuple2<String, Integer> createAccumulator() {
                                   return new Tuple2<>("",0);
                               }

                               @Override
                               public Tuple2<String, Integer> add(Tuple2<String, Integer> value, Tuple2<String, Integer> accumulator) {
                                   return new Tuple2<>(value.f0, accumulator.f1 + value.f1);
                               }

                               @Override
                               public Tuple2<String, Integer> getResult(Tuple2<String, Integer> accumulator) {
                                   return accumulator;
                               }

                               @Override
                               public Tuple2<String, Integer> merge(Tuple2<String, Integer> a, Tuple2<String, Integer> b) {
                                   return null;
                               }
                           })
                           .print();*/
                .process(new ProcessWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow>() {
                    @Override
                    public void process(String key, Context context, Iterable<Tuple2<String, Integer>> elements, Collector<Tuple2<String, Integer>> out) throws Exception {
                        System.out.println("context.window().getStart() = " + context.window().getStart());
                        long start = context.window().getStart();
                        Integer sum = 0;
                        for (Tuple2<String, Integer> element : elements) {
                            sum += element.f1;
                        }
                        out.collect(Tuple2.of(key, sum));
                        long end = context.window().getEnd();

                        System.out.println((end - start) / 1000L);
                    }
                })
                .print();

        env.execute();

    }
}
